using UnityEngine;
using System;

namespace UndergroundSurvival.Managers
{
    public class PlayerStatsManager : MonoBehaviour
    {
        public static PlayerStatsManager Instance { get; private set; }

        [Header("Player Stats")]
        [SerializeField] private float maxHunger = 100f;
        [SerializeField] private float maxSleep = 100f;
        [SerializeField] private float maxMental = 100f;
        [SerializeField] private float maxInfection = 100f;
        [SerializeField] private float maxTemperature = 100f;

        private float currentHunger;
        private float currentSleep;
        private float currentMental;
        private float currentInfection;
        private float currentTemperature;

        public event Action<float> OnHungerChanged;
        public event Action<float> OnSleepChanged;
        public event Action<float> OnMentalChanged;
        public event Action<float> OnInfectionChanged;
        public event Action<float> OnTemperatureChanged;

        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }

            InitializeStats();
        }

        private void InitializeStats()
        {
            currentHunger = maxHunger;
            currentSleep = maxSleep;
            currentMental = maxMental;
            currentInfection = 0f;
            currentTemperature = 50f; // 正常体温
        }

        private void Update()
        {
            UpdateStats(Time.deltaTime);
        }

        private void UpdateStats(float deltaTime)
        {
            // 基础消耗
            ModifyHunger(-0.1f * deltaTime);
            ModifySleep(-0.05f * deltaTime);
            ModifyMental(-0.02f * deltaTime);

            // 状态影响
            if (currentHunger < 20f)
            {
                ModifyMental(-0.1f * deltaTime);
            }
            if (currentSleep < 20f)
            {
                ModifyMental(-0.15f * deltaTime);
            }
        }

        public void ModifyHunger(float amount)
        {
            currentHunger = Mathf.Clamp(currentHunger + amount, 0f, maxHunger);
            OnHungerChanged?.Invoke(currentHunger / maxHunger);
        }

        public void ModifySleep(float amount)
        {
            currentSleep = Mathf.Clamp(currentSleep + amount, 0f, maxSleep);
            OnSleepChanged?.Invoke(currentSleep / maxSleep);
        }

        public void ModifyMental(float amount)
        {
            currentMental = Mathf.Clamp(currentMental + amount, 0f, maxMental);
            OnMentalChanged?.Invoke(currentMental / maxMental);
        }

        public void ModifyInfection(float amount)
        {
            currentInfection = Mathf.Clamp(currentInfection + amount, 0f, maxInfection);
            OnInfectionChanged?.Invoke(currentInfection / maxInfection);
        }

        public void ModifyTemperature(float amount)
        {
            currentTemperature = Mathf.Clamp(currentTemperature + amount, 0f, maxTemperature);
            OnTemperatureChanged?.Invoke(currentTemperature / maxTemperature);
        }

        public float GetStatPercentage(StatType statType)
        {
            return statType switch
            {
                StatType.Hunger => currentHunger / maxHunger,
                StatType.Sleep => currentSleep / maxSleep,
                StatType.Mental => currentMental / maxMental,
                StatType.Infection => currentInfection / maxInfection,
                StatType.Temperature => currentTemperature / maxTemperature,
                _ => 0f
            };
        }
    }

    public enum StatType
    {
        Hunger,
        Sleep,
        Mental,
        Infection,
        Temperature
    }
} 